import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/code_value_entity.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/mvp/svp_progress.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/util/toast.dart';
import './elder_care_send_page.dart';
import 'dart:convert';
import 'package:dio/dio.dart';

class ElderCareSelectedPage extends StatefulWidget {
  final int? customerId;
  final ElderInfo? info;
  ElderCareSelectedPage({Key? key, this.customerId, this.info});
  @override
  _ElderCareSelectedPageState createState() => _ElderCareSelectedPageState();
}

class _ElderCareSelectedPageState extends State<ElderCareSelectedPage> {
  List<CodeValue> dataList = [];

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _fectcodeType();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('选择照护类型'),
        elevation: 0.5,
      ),
      body: ListView.separated(
        itemCount: dataList.length,
        itemBuilder: (_, int index) {
          return ListTile(
            title: Text(dataList[index].nameCn??""),
            onTap: () {
              Navigator.pushReplacement(context,
                  new MaterialPageRoute(builder: (BuildContext context) {
                return ElderCareSendPage(
                  info: dataList[index],
                  customerId: widget.customerId!,
                  elderInfo: widget.info!,
                );
              }));
            },
          );
        },
        separatorBuilder: (_, int index) {
          return Divider();
        },
      ),
    );
  }

  _fectcodeType() async {
    var map = {'codeTypeId': 41};
    SVProgressHUD page = SVProgressHUD(context);
    HttpUtil.instance
        .getDio()
        .post(HttpApi.mapType, data: map,options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());

        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            CodeValueEntity model = CodeValueEntity.fromJson(map['data']);
            if ((model.rows??[]).length > 0) {
              setState(() {
                dataList = model.rows??[];
              });
            }
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      Toast.show(getErrorMessage(e));
    });
  }
}
